# -*- coding: utf-8 -*-
# @Time: 2021/11/17 21:04
# @Author: jiuwu
# @Email: 2359196586@qq.com
# @File: serializers.py
# @Software: PyCharm
from rest_framework import serializers
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
from .models import Label, Topic


class LabelSerializer(ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'


class TopicSerializer(ModelSerializer):
    # 如果只需要一个信息，可以用这种方式修改字段的输出方式
    label_name = serializers.CharField(source='label.name', read_only=True)
    # label_detail = SerializerMethodField()  # 修改外键字段的显示方式
    user_name = serializers.CharField(source='user.first_name', read_only=True)

    class Meta:
        model = Topic
        # fields = '__all__'
        exclude = ['is_delete']
        # extra_kwargs = {
        #     'label': {'write_only': True}
        # }

    # def get_label_detail(self, topic):
    #     serializer = LabelSerializer(topic.label)
    #     return serializer.data


class TopicNoAnswerSerializer(ModelSerializer):
    label_name = serializers.CharField(source='label.name', read_only=True)
    # label_detail = SerializerMethodField()  # 修改外键字段的显示方式
    user_name = serializers.CharField(source='user.first_name', read_only=True)

    class Meta:
        model = Topic
        # fields = '__all__'
        exclude = ['is_delete', 'answer']


class LabelTopicNoAnswerSerializer(ModelSerializer):  # 阶段测试用-标签题目序列化器-无答案版本
    topic_set = TopicNoAnswerSerializer(many=True, required=False, read_only=True)

    class Meta:
        model = Label
        fields = '__all__'
